package org.act.BPEL2Automata.automata.module;

import java.util.LinkedList;

import org.act.xservices.bpel11.TAssign;
import org.act.xservices.bpel11.TEmpty;
import org.act.xservices.bpel11.TFlow;
import org.act.xservices.bpel11.TInvoke;
import org.act.xservices.bpel11.TPick;
import org.act.xservices.bpel11.TReceive;
import org.act.xservices.bpel11.TReply;
import org.act.xservices.bpel11.TSequence;
import org.act.xservices.bpel11.TSwitch;
import org.act.xservices.bpel11.TTerminate;
import org.act.xservices.bpel11.TWait;
import org.act.xservices.bpel11.TWhile;
import org.eclipse.emf.ecore.EObject;

/**
 * definition of State,variable <b>stateID</b> is used for 
 * identify different State instances.
 * 
 * @author baoxiwu<bb@feijiao.info>,ACT,Beihang University
 * @version 0.9.0
 * @since 2010-03-23
 * 
 */
public class State {
	//static <variable>count</variable> to identify State instances.
	private static long count = 0l;
	
	private String stateID = "";
	
	//a list of references for recording what BPEL activity this automata be generated from.
	//ONLY start state and final state count.
	//Generic type is designated as EObject,'cause a mount of child objects of EObject need to be
	//added to the list. 
	private LinkedList<EObject> sourceList = new LinkedList<EObject>();
	private LinkedList<EObject> targetList = new LinkedList<EObject>();
	
	
	public State(){
		if(State.count < Long.MAX_VALUE){
			State.count ++;
			//copy current <variable>count</variable> as the ID of this State. 
			this.stateID = Long.toString(State.count);
		}
		else{
		//TODO too many object State instantiated.
		//common EXCEPTION_HANDLER WANTED.
		//System.err.println("TOO MANY STATEs INSTANTIATED...");
		}
	}
	
	
	public String getID(){
		return this.stateID;
	}
	
	
	//getter and setter methods.
	public void setSourceList(LinkedList<EObject> sourceList){
		this.sourceList = sourceList;
	}
	
	public void addSourceList(EObject source){
		this.sourceList.add(source);
	}
	
	public void addAllSourceList(LinkedList<EObject> sourceList){
		this.sourceList.addAll(sourceList);
	}
	
	public LinkedList<EObject> getSourceList(){
		return this.sourceList;
	}
	
	public void setTargetList(LinkedList<EObject> targetList){
		this.targetList = targetList;
	}
	
	public void addTargetList(EObject target){
		this.targetList.add(target);
	}
	
	public void addAllTargetList(LinkedList<EObject> targetList){
		this.targetList.addAll(targetList);
	}
	
	public LinkedList<EObject> getTargetList(){
		return this.targetList;
	}
	
	public String toString(){
		String result = "\nsource list:";
		
		for(int i = 0; i < this.getSourceList().size();i++){
			EObject eo = this.getSourceList().get(i);
			
			//1.assign activity
			if(eo instanceof TAssign){
				result += ("\n\tAssign[name=" + ((TAssign) eo).getName() + "]");
			}
			else
			//2.empty activity
			if(eo instanceof TEmpty){
				result += ("\n\tEmpty[name=" + ((TEmpty) eo).getName() + "]");
			}
			else
			//3.flow activity
			if(eo instanceof TFlow){
				result += ("\n\tFlow[name=" + ((TFlow) eo).getName() + "]");
			}
			else
			//4.invoke activity
			if(eo instanceof TInvoke){
				result += ("\n\tInvoke[name=" + ((TInvoke) eo).getName() + "]");
			}
			else
			//5.pick activity
			if(eo instanceof TPick){
				result += ("\n\tPick[name=" + ((TPick) eo).getName() + "]");
			}
			else
			//6.receive activity
			if(eo instanceof TReceive){
				result += ("\n\tReceive[name=" + ((TReceive) eo).getName() + "]");
			}
			else
			//7.reply activity
			if(eo instanceof TReply){
				result += ("\n\tReply[name=" + ((TReply) eo).getName() + "]");
			}
			else
			//8.switch activity
			if(eo instanceof TSwitch){
				result += ("\n\tSwitch[name=" + ((TSwitch) eo).getName() + "]");
			}
			else
			//9.terminate activity
			if(eo instanceof TTerminate){
				result += ("\n\tTerminate[name=" + ((TTerminate) eo).getName() + "]");
			}
			else
			//1i.wait activity
			if(eo instanceof TWait){
				result += ("\n\tWait[name=" + ((TWait) eo).getName() + "]");
			}
			else
			//11.while activity
			if(eo instanceof TWhile){
				result += ("\n\tWhile[name=" + ((TWhile) eo).getName() + "]");
			}
			else
			//12.sequence activity
			if(eo instanceof TSequence){
				result += ("\n\tSequence[name=" + ((TSequence) eo).getName() + "]");
			}
		}
		
		result += "\n\ntarget list:";
		for(int i = 0; i < this.getTargetList().size();i++){
			EObject eo = this.getTargetList().get(i);
			
			//1.assign activity
			if(eo instanceof TAssign){
				result += ("\n\tAssign[name=" + ((TAssign) eo).getName() + "]");
			}
			else
			//2.empty activity
			if(eo instanceof TEmpty){
				result += ("\n\tEmpty[name=" + ((TEmpty) eo).getName() + "]");
			}
			else
			//3.flow activity
			if(eo instanceof TFlow){
				result += ("\n\tFlow[name=" + ((TFlow) eo).getName() + "]");
			}
			else
			//4.invoke activity
			if(eo instanceof TInvoke){
				result += ("\n\tInvoke[name=" + ((TInvoke) eo).getName() + "]");
			}
			else
			//5.pick activity
			if(eo instanceof TPick){
				result += ("\n\tPick[name=" + ((TPick) eo).getName() + "]");
			}
			else
			//6.receive activity
			if(eo instanceof TReceive){
				result += ("\n\tReceive[name=" + ((TReceive) eo).getName() + "]");
			}
			else
			//7.reply activity
			if(eo instanceof TReply){
				result += ("\n\tReply[name=" + ((TReply) eo).getName() + "]");
			}
			else
			//8.switch activity
			if(eo instanceof TSwitch){
				result += ("\n\tSwitch[name=" + ((TSwitch) eo).getName() + "]");
			}
			else
			//9.terminate activity
			if(eo instanceof TTerminate){
				result += ("\n\tTerminate[name=" + ((TTerminate) eo).getName() + "]");
			}
			else
			//1i.wait activity
			if(eo instanceof TWait){
				result += ("\n\tWait[name=" + ((TWait) eo).getName() + "]");
			}
			else
			//11.while activity
			if(eo instanceof TWhile){
				result += ("\n\tWhile[name=" + ((TWhile) eo).getName() + "]");
			}
			else
			//12.sequence activity
			if(eo instanceof TSequence){
				result += ("\n\tSequence[name=" + ((TSequence) eo).getName() + "]");
			}
		}
		
		return result;
	}
}
